package com.blogs.web.shejimoshi.danli;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: xiaokai
 * @Description: 静态代理实现
 * @Date: 2019/8/11
 * @Version: 1.0
 */
public class OrderProxy {
    @Autowired
    private OrderService orderService;

    /**
     * @param order
     * @return
     */
    public int saveOrder(Order order) {
        beforeMethod();
        orderService = new OrderServiceImpl();
        int userId = order.getUserId();
        //通过静态代理分配处理数据
        int dbRouter = userId % 2;
        System.out.println("静态代理分配到【db" + dbRouter + "】 处理数据");


        afterMethod();
        return orderService.saveOrder(order);
    }

    /**
     * 通过静态代理监控业务开始与结束
     */
    private void beforeMethod() {
        System.out.println("静态代理 beforeMethod code");
    }

    public void afterMethod() {
        System.out.println("静态代理 afterMethod code");
    }
}
